package com.zbf.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zbf.dao.HtongDao;
import com.zbf.dao.SequenceUtilsDao;
import com.zbf.utils.PdfUtils;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.util.Map;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/6/9  19:48
 * 描述:
 */
@Component
public class HtongService {

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Autowired
    private SequenceUtilsDao sequenceUtilsDao;

    @Autowired
    private HtongDao htongDao;

    /**
     * 作者: LCG
     * 日期: 2021/6/9  19:50
     * 描述: 保存合同  并上传minio
     * @Param []
     * @Return java.lang.String
     */
    @Transactional(rollbackFor = Exception.class,timeout = 20000)
    public Map<String, String> saveht(Map<String,Object> htMap) throws Exception {

        //合同编号
        String htcode = sequenceUtilsDao.getSequence(1);
        htMap.put("htcode",htcode);

        //上传minio
        String fileName=htMap.get("title").toString()+".pdf";
        Map<String, String> map = PdfUtils.uploadToMonio(fileName, minioClient, "images2", freeMarkerConfigurer, "hetong.html", htMap);
        map.put("htcode",htcode);
        htMap.put("fileName",fileName);
        htMap.put("filePath",map.get("filePath"));

        //调用签章接口,进行签章
        String server = PdfUtils.remoteServer(htMap, "http://localhost:20000/addQianZhang");
        JSONObject jsonObject = JSON.parseObject(server);
        if(jsonObject.get("code").toString().equals("200")){
            //存数据库
            htongDao.insertIntoHt(htMap);
        }else{
            throw new RuntimeException("调用签章失败！");
        }

        return map;

    }




}
